﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManagementSystem
{
    /// <summary>
    /// 登录窗体
    /// </summary>
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 取消登录操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

            //关闭窗体
            this.Close();
        }

        /// <summary>
        /// 登录操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (login())
            {
                //启动计时器
                timer1.Start();

                //隐藏文本内容
                textBoxID.Visible = false;
                textBoxPwd.Visible = false;
                comboBox1.Visible = false;
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                buttonCancel.Visible = false;
                buttonOK.Visible = false;
            }
        }

        /// <summary>
        /// 登录函数
        /// </summary>
        /// <returns></returns>
        private bool login()
        {
            //检查是否有信息为空，提示用户输入完整
            if (textBoxID.Text == "" || textBoxPwd.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("输入信息不完整，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            //当用户选择“管理员”身份登录时
            else if (comboBox1.Text == "管理员")
            {
                //验证管理员信息是否正确
                if (textBoxID.Text == "adminYXY" && textBoxPwd.Text == "adminYXY")
                {
                    return true;
                }

                //不正确则提示输入信息有误
                else
                {
                    MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }

            //当用户选择“学生”身份登录时
            else if (comboBox1.Text == "学生")
            {
                //调用函数从数据库中查询输入信息是否存在且属实
                string sql = "select * from Student where ID='" + textBoxID.Text + "'and Password='" + textBoxPwd.Text + "'";
                Database database = new Database();
                IDataReader dr = database.read(sql);
                if (dr.Read())
                {
                    return true;
                }

                //不正确则提示输入信息有误
                else
                {
                    MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }

            //当用户选择“教师”身份登录时
            else if (comboBox1.Text == "教师")
            {
                //调用函数从数据库中查询输入信息是否存在且属实
                string sql = "select * from Teacher where ID='" + textBoxID.Text + "'and Password='" + textBoxPwd.Text + "'";
                Database database = new Database();
                IDataReader dr = database.read(sql);
                if (dr.Read())
                {
                    return true;
                }

                //不正确则提示输入信息有误
                else
                {
                    MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }

            //返回输入信息有误
            else
            {
                MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        /// <summary>
        /// 启用计时器，移动图片，并加载出新窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //将图片移至窗体中心后停止
            if (pictureBox1.Location.X < 200)
            {
                //只改变横坐标，不改变纵坐标，即图片水平移动
                pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);
            }
            else
            {
                Form nextForm = null;

                //根据用户身份选择打开对应的窗体
                switch (comboBox1.Text)
                {
                    case "学生":
                        nextForm = new StudentSelect();
                        break;
                    case "教师":
                        nextForm = new TeacherView();
                        break;
                    case "管理员":
                        nextForm = new FormAdmin();
                        break;
                }
                if (nextForm != null)
                {
                    // 显示新的窗体
                    nextForm.Show();

                    // 设置新窗体的 Shown 事件处理程序
                    nextForm.Shown += (s, ev) =>
                    {
                         // 确保新窗体完全显示后隐藏当前窗体
                         this.Hide();
                    };

                    //停止计时器
                    timer1.Stop();
                }
                timer1.Stop();
            }
        }
    }
}
